<?php
namespace Text\Controller;
use Think\Controller;

class IndexController extends Controller {
	//public $dataa;
    public function index(){
       echo "admin";
    }
 
	
	public function videos(){
		$videosmodel=M('videos');
		$videos=$videosmodel->order('vid desc')->select();
		$this->videos=$videos;
		$this->display();
	}

	
		 function teacher($id){
		$videosmodel=M("videos");
		$this->videos=$videosmodel->find($id);
		$this->display();
	}

	function del($id){
		$videosmodel=M("videos");
		if($videosmodel->delete($id)){
			$this->success('删除成功');
		}
		else{
			$this->error('删除失败');
		}
	}
	

	function update(){
		  //I(POST.) ==$_POST[""]
		$videosmodel=D("videos");
		$videosmodel->create();
		 if ($_FILES['pic']['error']==0) {//有文件上传则执行，否则直接过。
				 $photoinfo=$this->upload();	
				 $videosmodel->pic=$photoinfo['pic']['savename'];
			 }	
		if($videosmodel->save()){
					$this->success('修改成功',"videos");
		}
		else{
			$this->error('修改失败');
		}
	  }
	

	   function upload(){
			$upload = new \Think\Upload();// 实例化上传类
			$upload->maxSize = 3145728 ;// 设置附件上传大小
			$upload->exts = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
			$upload->rootPath = './Public/uploads/photo/'; // 设置附件上传根目录
			//$upload->savePath = ''; // 设置附件上传（子）目录
			// 上传文件
			$upload->autoSub=false;
			$info = $upload->upload();
			if(!$info) {// 上传错误提示错误信息
			$this->error($upload->getError());
			}else{// 上传成功
				return $info;
			}
		}



		function img(){	
				 $photoinfo=$this->upload();
				 $filePath='./Public/uploads/photo/';
				 $oldFilename=$photoinfo['pic']['savename'];
				 $oldFullname=$filePath.$oldFilename;
				 $newFilename='thumb_'.$oldFilename;
				 $newFullname=$filePath.$newFilename;//这一堆，慢慢看吧
				 $image=new \Think\Image();//生成图片类
				 $image->open($oldFullname);//打开文件
				 $image->thumb(100,100)->save($newFullname);
				 return $newFilename;

			
		}


		function addVideos(){
			$videosmodel=D("videos");
		
			if($videosmodel->create()){
			if ($_FILES['pic']['error']==0) {
				$fileInfo=$this->img();
				$videosmodel->pic=$fileInfo;
				}
			if($videosmodel->add()){
					$this->success('添加成功',"videos");
			}
			else{
				$this->error('添加失败');
			}
		  }
		  else{
		  	$this->error($videosmodel->getError());
		  }

		}



}